const { UserModel, VerificationModel } = require('../commons/model')
const http = require('../utils/http')
module.exports = {
  async login(data) {
    let { tel, code } = data
    if (!tel || !code) {
      return { suc: false }
    }
    let code = ''
    for (let i = 0; i < 6; i++) {
      code += Math.floor(Math.random() * 10)
    }
    await VerificationCode.saveCode({ telephone, code })
    // 发送短信验证码
    try {
      const res = await http.gets('http://www.4001185185.com/sdk/smssdk!mt.action', {
        sdk: '61255',
        code: 'dd61255',
        pwdtype: 'md5',
        phones: '18914223026',
        msg: '您的验证码为',
        encode: 'UTF-8',
        resulttype: 'xml',
        subcode: '1234'
      })
      console.log('短信验证码发送成功', res.data)
      let res = await userModel.login(tel, code)
      return res
    } catch (e) {
      console.log(e)
      return { suc: false }
    }
  }
}
